using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Dinner.Models;

namespace Dinner.Controllers
{
	public class MenuController : Controller
	{
		MenuRepository menuRepository = new MenuRepository(); 

		//
		// GET: /Menu/
		public ActionResult Index()
		{
			var menus = menuRepository.FindUpcomingMenus().ToList();
			
			return View(menus);
		}

		//
		// GET: /Menu/Details/5
		public ActionResult Details(int id)
		{
			Menu menu = menuRepository.GetMenu(id);

			if (menu == null)
				return View("NotFound");
			else
				return View(menu);
		}


		//
		// GET: /Menu/Create
		public ActionResult Create()
		{
			return View();
		} 

		//
		// POST: /Menu/Create
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(FormCollection collection)
		{
			Menu menu = new Menu();
			try
			{
				UpdateModel(menu);

				menuRepository.Add(menu);
				menuRepository.Save();

				return RedirectToAction("AddItems", new {id = menu.menu_id});
			}
			catch
			{
				ModelState.AddRuleViolations(menu.GetRuleViolations());
				return View(menu);
			}
		}

		//
		// GET: /Menu/Edit/5
		public ActionResult Edit(int id)
		{
			Menu menu = menuRepository.GetMenu(id);

			return View(menu);
		}

		//
		// POST: /Menu/Edit/5
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(int id, FormCollection collection)
		{
			Menu menu = menuRepository.GetMenu(id);

			try
			{
				UpdateModel(menu);
				menuRepository.Save();
				return RedirectToAction("Details", new { id = menu.menu_id });
			}
			catch
			{
				ModelState.AddRuleViolations(menu.GetRuleViolations());
				return View(menu);
			}
		}

		//
		// GET: /Menu/AddItems/5
		public ActionResult AddItems(int id)
		{
			Menu menu = menuRepository.GetMenu(id);
			
			if (menu != null)
			{
				var itemRepository = new ItemRepository();
				ViewData["availableItems"] = itemRepository.GetAllSelectItems();
				ViewData["menuId"] = id;
				return View();
			}
			return View("NotFound");
		}

		//
		// POST: /Menu/AddItems/5
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult AddItems(int id, FormCollection collection)
		{
			Menu menu = menuRepository.GetMenu(id);

			try
			{
				var selectedItems = GetSelectedItems();
				foreach (var item in selectedItems)
				{
					var menuItem = new MenuItem {item_id = item.item_id, menu_id = id};
					menu.MenuItems.Add(menuItem);

					menuRepository.Save();
				}
				return RedirectToAction("Details", new { id = menu.menu_id });
			}
			catch
			{
				ModelState.AddRuleViolations(menu.GetRuleViolations());
				return View(menu);
			}
		}

		#region Private Helpers

		private List<Item> GetSelectedItems()
		{
			var result = new List<Item>();
			
			String selectedItemsString = Request["selectedItems"];
			if (string.IsNullOrEmpty(selectedItemsString))
				return result;

			var itemRepository = new ItemRepository();
			string[] itemIds = selectedItemsString.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries);
			foreach (var id in itemIds)
			{
				Item item = itemRepository.GetItem(int.Parse(id));
				result.Add(item);
			}

			return result;
		}

		#endregion
	}
}
